//
//  shangjiaViewController.m
//  BAOASystem
//
//  Created by michael on 2018/2/22.
//  Copyright © 2018年 czd. All rights reserved.
//
#import "zjyoupinViewController.h"
#import "LXSegmentBtnView.h"
#import "shangjiaViewController.h"
#import "caigouViewController.h"
#import "gongyingViewController.h"
@interface shangjiaViewController ()<LXSegmentBtnViewDelegate>{
    gongyingViewController *_oneVC;
    caigouViewController *_twoVC;

    
}
@property (nonatomic , strong) LXSegmentBtnView *segmentView1;

@end

@implementation shangjiaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
     NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:

[UIColor whiteColor],NSForegroundColorAttributeName, nil];
 
[self.navigationController.navigationBar setTitleTextAttributes:attributes];
 self.navigationController.navigationBar.barTintColor = [UIColor colorWithHexString:@"0a62ea"];
//    UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:@[NSLocalizedString(@"商家-供应商", nil),NSLocalizedString(@"商家-采购商", nil)]];
//    segment.segmentedControlStyle = UISegmentedControlStyleBar;
//[segment setWidth:100 forSegmentAtIndex:1];
//[segment setWidth:100 forSegmentAtIndex:0];
//
//
//    [segment setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"0a62ea"]}forState:UIControlStateSelected];
//
//    [segment setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]}forState:UIControlStateNormal];
//
//    segment.tintColor = [UIColor colorWithHexString:@"fbf2f2"];
//[segment setBackgroundImage:[UIImage imageNamed:@"fbf2f2"]
//                          forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
//
//        segment.backgroundColor = [UIColor colorWithHexString:@"fbf2f2"alpha:0.32];
//
//
//    segment.layer.borderWidth = 0;
//
// segment.selectedSegmentIndex = 0;
//    [segment addTarget:self action:@selector(segmentClick:) forControlEvents:UIControlEventValueChanged];
//    self.navigationItem.titleView = segment;
       _segmentView1 = [[LXSegmentBtnView alloc] init];
        _segmentView1.delegate = self;
        _segmentView1.btnBackgroundSelectColor = [UIColor colorWithHexString:@"fbf2f2"];
        _segmentView1.titleFont = [UIFont systemFontOfSize:14];
self.segmentView1.frame =CGRectMake ((KScreenWidth - 200)/2,  40, 200, 30);;
    self.segmentView1.btnTitleArray = @[NSLocalizedString(@"商家-供应商", nil),NSLocalizedString(@"商家-采购商", nil)];
        self.navigationItem.titleView = _segmentView1;
__block typeof(self) bself = self;
     self.segmentView1.lxSegmentBtnSelectIndexBlock = ^(NSInteger index) {
        NSLog(@"block , index = %zd" , index);
        if(index ==0){
             [bself replaceFromOldViewController:_twoVC toNewViewController:_oneVC];

//          [bself.view addSubview:_oneVC.view];
//            [_twoVC.view removeFromSuperview];
        }
        if(index ==1){
                    [bself replaceFromOldViewController:_oneVC toNewViewController:_twoVC];

//          [bself.view addSubview:_twoVC.view];
//            [_oneVC.view removeFromSuperview];
        }
    };
    
    self.view.backgroundColor = [UIColor whiteColor];
     _oneVC = [[gongyingViewController alloc] init];
//    _oneVC.view.backgroundColor = [UIColor redColor];
    _twoVC = [[caigouViewController alloc] init];
    [self addChildViewController:_oneVC];
    [self addChildViewController:_twoVC];
  self.currentVC = _oneVC;

//    _twoVC.view.backgroundColor = [UIColor grayColor];
    [self.view addSubview:_oneVC.view];
}

//-(void)segmentClick:(UISegmentedControl *)segment{
//    
//    switch (segment.selectedSegmentIndex) {
//        case 0:
//            //第一个界面
//            [self.view addSubview:_oneVC.view];
//            [_twoVC.view removeFromSuperview];
//            break;
//        case 1:
//            [self.view addSubview:_twoVC.view];
//            [_oneVC.view removeFromSuperview];
//            
//            
//            break;
//        default:
//            break;
//    }
//    }
 - (void)replaceFromOldViewController:(UIViewController *)oldVc toNewViewController:(UIViewController *)newVc{
    /**
     *  transitionFromViewController:toViewController:duration:options:animations:completion:
     *  fromViewController    当前显示在父视图控制器中的子视图控制器
     *  toViewController        将要显示的姿势图控制器
     *  duration                动画时间(这个属性,old friend 了 O(∩_∩)O)
     *  options              动画效果(渐变,从下往上等等,具体查看API)UIViewAnimationOptionTransitionCrossDissolve
     *  animations            转换过程中得动画
     *  completion            转换完成
     */
    [self addChildViewController:newVc];
    [self transitionFromViewController:oldVc toViewController:newVc duration:0.1 options:UIViewAnimationOptionTransitionCrossDissolve animations:nil completion:^(BOOL finished) {
        if (finished) {
            [newVc didMoveToParentViewController:self];
            [oldVc willMoveToParentViewController:nil];
            [oldVc removeFromParentViewController];
            self.currentVC = newVc;
        }else{
            self.currentVC = oldVc;
        }
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
